package com.yuki.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yuki.api.annotation.LogType;
import com.yuki.api.annotation.SystemLog;
import com.yuki.api.annotation.UserLoginToken;
import com.yuki.api.common.CommonPage;
import com.yuki.api.common.CommonResult;
import com.yuki.api.dto.OmsOrderQueryParam;
import com.yuki.api.model.OmsOrder;
import com.yuki.api.service.IOmsOrderService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: Yuki Tadayoshi
 * @time: 2021/3/30 20:14
 */
@RestController
@RequestMapping("/order")
@CrossOrigin
public class OmsOrderController {
    @Reference(
            version = "1.0.0",
            interfaceName = "com.yuki.api.service.IOmsOrderService",
            interfaceClass = IOmsOrderService.class,
            timeout = 120000
    )
    private IOmsOrderService service;

    @GetMapping("/list")
    @ResponseBody
    @UserLoginToken
    @SystemLog(description = "查询订单", type = LogType.ORDER_LIST)
    public CommonResult<CommonPage<OmsOrder>> list(
            OmsOrderQueryParam queryParam,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return CommonResult.success(service.list(queryParam, pageSize, pageNum));
    }

    @GetMapping("/{id}")
    @ResponseBody
    @UserLoginToken
    @SystemLog(description = "订单明细", type = LogType.ORDER_DETAIL)
    public CommonResult<OmsOrder> detail(@PathVariable Long id) {
        return CommonResult.success(service.detail(id));
    }

    @PostMapping("/delete")
    @ResponseBody
    @UserLoginToken
    @SystemLog(description = "删除订单", type = LogType.ORDER_DELETE)
    public CommonResult<OmsOrder> delete(@RequestParam("ids") List<Long> ids) {
        CommonResult commonResult;
        try {
            ids.forEach(c -> service.delete(c));
            commonResult = CommonResult.success(1);
        } catch (Exception e) {
            commonResult = CommonResult.failed();
            e.printStackTrace();
        }
        return commonResult;
    }

    @PostMapping("/cancelOrder")
    @ResponseBody
    @UserLoginToken
    @SystemLog(description = "取消单个超时订单",type = LogType.ORDER_CANCEL)
    public CommonResult cancelOrder(@RequestParam("ids") List<Long> ids,@RequestParam("minute") int minute){
        service.sendDelayMessageCancelOrder(ids.get(0),minute);
        return CommonResult.success(null);
    }


}
